# python-resmon - Python tools for Resmon monitoring and the Circonus API
# Copyright (c) 2010 Crown coypright
# 
# This file is part of python-resmon.
# 
# python-resmon is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# python-resmon is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with python-resmon.  If not, see <http://www.gnu.org/licenses/>.

from ruleparse.parser import *

from ruleparse.loader import load

import os
os.environ['DJANGO_SETTINGS_MODULE'] = '__main__'

TIME_ZONE = 'Europe/London'

from django.template import Context, Template

filename = 'rules.cm'
nodes = load(filename)
nodes.sort(key=lambda node: node.hostname)
context = Context({'nodes': nodes, 'filename': filename})
t = Template(open('templates/rule2html.html').read())

f = open('ruledoc/index.html', 'w')
f.write(t.render(context))
f.close()

by_sev = {}

for host in nodes:
    for metric in host.get_metrics():
        for rule in metric.get_rules():
            by_sev.setdefault(rule.severity, []).append((host, metric, rule))

by_sev = list(by_sev.items())
by_sev.sort()

context = Context({'by_sev': by_sev, 'filename': filename})
f = open('ruledoc/by_sev.html', 'w')
t = Template(open('templates/by_sev.html').read())
f.write(t.render(context))
f.close()
